// pages/car/car.js
const {
  Car
} = require("../../utils/api")

const {
  Http
} = require("../../utils/http")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Allchecked: false, //全选反选
    checked: true,
    shopCarList: [], //购物车列表
    value: 1,
    count: 0, //商品总量
    finalMoney: 0,
    subtractMoney: 0,
    totalMoney: 0, //总价
  },
  //全选
  doubleonChange(event) {
    //重新赋值购物车列表
    let shopCarList = this.data.shopCarList
    //当前状态
    let Allchecked = this.data.Allchecked;
    Allchecked = !Allchecked
    // this.data.Allchecked=!Allchecked
    console.log(Allchecked)
    shopCarList = shopCarList.map(item => {
      return {
        ...item,
        checked: Allchecked
      }

    })
    this.setData({
      shopCarList: shopCarList,
      Allchecked: Allchecked
    }, () => {
      console.log(this.data.Allchecked)
    });
    this.callAllPrice()
  },
  //单选
  onChange(event) {
    console.log(event)
    //获取当前点击的下标
    let index = event.currentTarget.dataset.index;
    //重新赋值购物车列表
    const shopCarList = this.data.shopCarList
    //取出当前状态
    let checked = shopCarList[index].checked
    shopCarList[index].checked = !checked
    // console.log(!checked)
    this.setData({
      shopCarList: shopCarList,
    });
    this.isAllcheck()
    this.callAllPrice()
  },

  //判断是否全选
  isAllcheck() {
    let shopCarList = this.data.shopCarList
    const Allchecked = shopCarList.every(item => item.checked === true)
    console.log(Allchecked)
    this.setData({
      Allchecked: Allchecked
    })
  },

  //商品减减
  downCount(e) {
    console.log(e)
    //点击的下标
    const index = e.currentTarget.dataset.index;
    //购物车信息
    const shopCarList = this.data.shopCarList
    const shopItem = this.data.shopCarList[index]
    console.log(shopItem)
    this.carShop(shopItem, index, -1)
    this.setData({
      shopCarList: shopCarList,
    });
    this.carShop()
  },


  //商品加加
  addCount(e) {

    const index = e.currentTarget.dataset.index;
    //购物车信息
    const shopItem = this.data.shopCarList[index]
    this.carShop(shopItem, index, 1)
  },

  //购物车计算
  carShop(shopItem, index, count) {
    // const shopItem = this.data.shopCarList
    // const checkList = shopList.filter(item => item.checked);
    // const basketIds = checkList.map(item => item.basketId);
    const data = {
      basketId: 0,
      count,
      prodId: shopItem.prodId,
      shopId: shopItem.shopId,
      skuId: shopItem.skuId,
    }
    Car.setCar(data, (res) => {
      console.log(res)
      if (res.data == "库存不足") {
        wx.showToast({
          title: '库存不足',
          icon: "error"
        })
      }
      this.getCarShop()
    })
  },

  //商品总价计算
  // 结算----计算总价
  callAllPrice() {
    const shopList = this.data.shopCarList;
    console.log(shopList)
    // 过滤选中的数据
    const checkList = shopList.filter(item => item.checked);
    const basketIds = checkList.map(item => item.basketId);
    Http('/p/shopCart/totalPay', basketIds, 'POST', (res) => {
      console.log(res.data.totalMoney);
      this.setData({
        count: res.data.count, //商品总量
        finalMoney: res.data.finalMoney,
        subtractMoney: res.data.subtractMoney,
        totalMoney: res.data.totalMoney, //总价
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCarShop()
    console.log(this.data.shopCarList)
    // this.getCarShop()
  },
  onShow() {
    this.getCarShop()
  },
  //获取购物车数据
  getCarShop() {
    Car.getCarList({}, (res) => {
      console.log(res)
      const shopCarList = res.data[0].shopCartItemDiscounts[0].shopCartItems.map((item) => {
        return {
          ...item,
          checked: false
        }
      })
      this.setData({
        shopCarList: shopCarList,
        Allchecked: false
      })
      console.log(this.data.shopCarList)
    })
  },

  //提交订单
  onClickButton(e) {
    const shopList = this.data.shopCarList;
    //过滤选中的数据
    const checkList = shopList.filter(item => item.checked);
    //选中选中状态下的商品id
    const basketIds = checkList.map(item => item.basketId)
    //商品id存到本地
    wx.setStorageSync('basketIds', basketIds)
    console.log(e)
    wx.navigateTo({
      url: '/pages/orderpage/orderpage?orderEntry=0',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})